package com.mobile.tools;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;
import org.hibernate.type.Type;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import com.mobile.model.TemplateTable;

/**
 * 修改hibernate的映射文件
 * @author hf
 *
 */
public class HibernateMappingManager {
	/**
	 * 更新映射文件
	 * @param mappingName			映射文件名
	 * @param propertiesList		映射文件的数据
	 */
	public void updateClassMapping(String mappingName,List<Map<String,String>> propertiesList){
		String filePath=TemplateTable.class.getResource(mappingName).getPath();
		this.updateClassMapping(propertiesList, filePath);
	}
	/**
	 * 更新映射文件
	 * @param propertiesList		映射文件的数据
	 * @param filePath				映射文件路径
	 */
	public void updateClassMapping(List<Map<String,String>> propertiesList,String filePath){
		try {
			
			Document document=XMLUtil.loadDocument(filePath);
			NodeList componentTags=document.getElementsByTagName("dynamic-component");
			Node node=componentTags.item(0);
			XMLUtil.removeChildren(node);
			
			for(Map<String,String> map:propertiesList){
				Element element=creatPropertyElement(document,map);
				node.appendChild(element);
			}		
			//保存dom
			XMLUtil.saveDocument(document, filePath);
			//XMLUtil.saveDocument(null, null);
		} catch (DOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(TransformerException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新映射文件
	 * @param hem					HibernateEntityMananger实例
	 */
	public void updateClassMapping(HibernateEntityManager hem){
		String file=hem.getHbmXmlPath();
		try {
			//String file=TemplateTable.class.getResource(hem.getMappingName()).getPath();
			//Configuration con=HibernateSessionFactory.getConfiguration();
			//con.addResource(file);
			//PersistentClass pc=HibernateSessionFactory.getClassMapping("EDU_HALL_DATA_01");
			
			Document document=XMLUtil.loadDocument(file);
			NodeList componentTags=document.getElementsByTagName("dynamic-component");
			Node node=componentTags.item(0);
			XMLUtil.removeChildren(node);
			
			Iterator propertyIterator=hem.getEntityProperties().getPropertyIterator();
			while(propertyIterator.hasNext()){
				Property property=(Property)propertyIterator.next();
				Element element=creatPropertyElement(document,property);
				node.appendChild(element);
			}
			
			XMLUtil.saveDocument(document, file);
		} catch (DOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(TransformerException e){
			e.printStackTrace();
		}
	}
	/**
	 * 创建xml的属性的对象
	 * @param document
	 * @param property
	 * @return
	 */
	private Element creatPropertyElement(Document document,Property property){
		Element element=document.createElement("property");
		Type type=property.getType();
		
		element.setAttribute("name", property.getName());
		element.setAttribute("column", ((Column)property.getColumnIterator().next()).getName());
		element.setAttribute("type", type.getReturnedClass().getName());
		element.setAttribute("not-null", String.valueOf(false));
		
		return element;
	}
	/**
	 * 创建xml的属性的对象
	 * @param document
	 * @param map
	 * @return
	 */
	private Element creatPropertyElement(Document document,Map<String,String> map){
		Element element=document.createElement("property");
		
		element.setAttribute("name", map.get("name"));
		element.setAttribute("column", map.get("column"));
		element.setAttribute("type", map.get("type"));
		element.setAttribute("not-null", String.valueOf(false));
		
		return element;
	}
	/**
	 * 修改映射文件的实体名和表名
	 * @param mappingName
	 * @param entityName
	 */
	public void updateEntityName(String file,String entityName){
		try {
			Document document=XMLUtil.loadDocument(file);
			NodeList nodeList=document.getElementsByTagName("class");
			Element element=(Element)nodeList.item(0);
			element.setAttribute("entity-name",entityName);
			element.setAttribute("table", entityName);
			nodeList=document.getElementsByTagName("param");
			Element elementParam=(Element)nodeList.item(0);
			XMLUtil.removeChildren(elementParam);
			Text text=document.createTextNode(entityName+"_SEQ");
			elementParam.appendChild(text);	
			XMLUtil.saveDocument(document, file);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 更新hibernate的配置文件
	 * @param configName	配置文件名
	 * @param mappingName	映射名称
	 * @param resource		hbm.xml文件路径
	 * 
	 */
	public void updateHibernateConfig(String configName,String mappingName,String resource){
		//获取应用服务器中的hibernate的配置文件
		//String file=Thread.currentThread().getContextClassLoader().getResource(configName).getPath();
		//测试用的获取hibernate的配置文件
		String file=Thread.currentThread().getContextClassLoader().getResource(configName).getPath();
		/*String resource=TemplateTable.class.getResource(mappingName).toString();
		String classPath=Thread.currentThread().getContextClassLoader().getResource("").toString();
		resource=resource.substring(classPath.length());*/
		try {
			Document document=XMLUtil.loadDocument(file);
			NodeList nodeList=document.getElementsByTagName("session-factory");
			Element element=(Element)nodeList.item(0);
			Element elementNew=document.createElement("mapping");
			elementNew.setAttribute("resource",resource);
			Text text=document.createTextNode("");
			element.appendChild(text);
			element.appendChild(elementNew);
			XMLUtil.saveDocument(document, file);
			//XMLUtil.saveDocument(null, null);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e){
			e.printStackTrace();
		}
	}
}